<?php

require 'config.php';

$fromfolder = $_REQUEST["fromfolder"];
$tofolder = $_REQUEST["tofolder"];
$filename = $_REQUEST["filename"];

if (empty($fromfolder) || empty($tofolder) || empty($filename)) {
    echo "miss parame.";
    die;
}
$dir_root = dirname(__FILE__);
$fromfolder = $dir_root . "/" . $fromfolder;
$tofolder = $dir_root . "/" . $tofolder;
try {
    $a_file = explode(",", $filename);
    if (!empty($a_file)) {
        foreach ($a_file as $file) {
            if (!empty($file)) {
                // create folder
                $a_folderdate = explode("/", $file);
                if (count($a_folderdate) > 1) {
                    unset($a_folderdate[count($a_folderdate) - 1]);
                    $str_folderdate = implode("/", $a_folderdate);
                    if (!is_dir($tofolder . DIRECTORY_SEPARATOR . $str_folderdate)) {
                        mkdir($tofolder . DIRECTORY_SEPARATOR . $str_folderdate, 0777, true);
                    }
                }
                if (is_file($fromfolder . "/" . $file)) {
                    if (copy($fromfolder . "/" . $file, $tofolder . "/" . $file) or die("Unable to copy $old to $new.")) {
                        unlink($fromfolder . "/" . $file);
                    }
                }
            }
        }
    }
} catch (Exception $e) {
    echo "error:" . DIRECTORY_SEPARATOR;
    die;
}
echo "success";
die;
?>
